<?php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}

function buildGlossary($page) {
  $glossary = array();
  $csv = csv_to_array('glossary.csv');
  for ($i = 0; $i < count($csv); $i++) {
    $t = $csv[$i]['term'];
    $tNoSpace = str_replace(' ','',$t);
    $d = $csv[$i]['definition'];
    if ($csv[$i]['page'] == $page) {
       $a = <<<EOHTML
<strong><a id="glossary_t_$tNoSpace" style="cursor: default;" href="">$t</a></strong>
EOHTML;
       $div = <<<EOHTML
<div id="glossary_d_$tNoSpace" style="z-index: 1000; margin-left: 200px; margin-top: 0px; display: none; position: absolute; width: 275px; border: 4px solid #c9c9c9; padding: 10px; background-color: #f2f2f2;">
  <p style="font-weight: bold; font-style: italic; margin-bottom: 0;">$d</p>
</div>
EOHTML;
       $glossary[$t] = array(
          'a'   => $a
         ,'div' => $div
       );
    }
  }
  return $glossary;
}

?>
